package com.lw.extraceim.app;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;

import com.lw.extraceim.loader.UserLoader;
import com.lw.extraceim.model.UserInfo;

import java.util.Map;
import java.util.Set;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : app的配置类
 * @Date : 2019/4/11 10:34
 */
public class ExtraceCfg {

    private static final String EXTRACE_CFG = "ExTrace.cfg";//app配置文件
    private static final String USER_LOGIN_INFO = "user.cfg";//用户登录信息
    private final static String REMEMBER_ME = "remember_me";
    private final static String AUTO_LOGIN = "auto_login";
    private Context context;

    private boolean rememberMe; // 是否记住用户登录账号和密码
    private boolean autoLogin;//是否自动登录

    private ExtraceCfg() {
        super();
    }

    public boolean isRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }

    public boolean isAutoLogin() {
        return autoLogin;
    }

    public void setAutoLogin(boolean autoLogin) {
        this.autoLogin = autoLogin;
    }


    public void save() {
        SharedPreferences settings = context.getSharedPreferences(EXTRACE_CFG,  Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(REMEMBER_ME, this.rememberMe);
        editor.putBoolean(AUTO_LOGIN, this.autoLogin);
        editor.commit();
    }

    public static ExtraceCfg getExtraceCfg(Context context) {
        SharedPreferences settings = context.getSharedPreferences(EXTRACE_CFG,  Context.MODE_PRIVATE);
        ExtraceCfg extraceCfg = new ExtraceCfg();
        extraceCfg.setRememberMe(settings.getBoolean(REMEMBER_ME, false));
        extraceCfg.setAutoLogin(settings.getBoolean(AUTO_LOGIN, false));
        extraceCfg.setContext(context);
        return extraceCfg;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public UserLoginInfo getUserLoginInfo() {
        SharedPreferences settings = context.getSharedPreferences(USER_LOGIN_INFO,  Context.MODE_PRIVATE);
        UserLoginInfo userLoginInfo = new UserLoginInfo(settings.getString("phone", ""), settings.getString("password", ""));
        return userLoginInfo;
    }

    public void setUserLoginInfo (String phone,String password){
        UserLoginInfo userLoginInfo = new UserLoginInfo(phone,password);
        if (userLoginInfo != null) {
            SharedPreferences settings = context.getSharedPreferences(USER_LOGIN_INFO,  Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("phone", userLoginInfo.phone);
            editor.putString("password", userLoginInfo.password);
            editor.commit();
        }

    }

    @Override
    public String toString() {
        return "ExtraceCfg{" +
                "context=" + context +
                ", rememberMe=" + rememberMe +
                ", autoLogin=" + autoLogin +
                '}';
    }

    public class UserLoginInfo {
        public String phone;
        public String password;

        public UserLoginInfo(String phone, String password) {
            this.phone = phone;
            this.password = password;
        }

        @Override
        public String toString() {
            return "UserLoginInfo{" +
                    "phone='" + phone + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }

}
